﻿param($servers,$credential,$exportDirectory,$exportFilePrefix,$featuresOrRolesToValidate)

Import-Module -Force ..\Windows\libWindowsUpdate.psm1
$scriptRootDirectory=Split-Path -Parent $PSCommandPath

$files=Get-content $scriptRootDirectory\CoreFiles.txt

Remove-Item -ErrorAction SilentlyContinue -Force $exportDirectory\$exportFilePrefix`_CPUInformation.csv
Remove-Item -ErrorAction SilentlyContinue -Force $exportDirectory\$exportFilePrefix`_PhysicalDisk.csv
Remove-Item -ErrorAction SilentlyContinue -Force $exportDirectory\$exportFilePrefix`_EnclosureInformation.csv
Remove-Item -ErrorAction SilentlyContinue -Force $exportDirectory\$exportFilePrefix`_SCSIControllerDriverInformation.csv 
Remove-Item -ErrorAction SilentlyContinue -Force $exportDirectory\$exportFilePrefix`_NIC_Information.csv
Remove-Item -ErrorAction SilentlyContinue -Force $exportDirectory\$exportFilePrefix`_Recommended_WindowsUpdates.csv
Remove-Item -ErrorAction SilentlyContinue -Force $exportDirectory\$exportFilePrefix`_BIOS_Information.csv
Remove-Item -ErrorAction SilentlyContinue -Force $exportDirectory\$exportFilePrefix`_Hotfix_Information.csv
Remove-Item -ErrorAction SilentlyContinue -Force $exportDirectory\$exportFilePrefix`_CoreFiles_Information.csv

foreach($server in $servers)
{
    Write-Host "Gathering information for $server"
    $session=New-PSSession -ComputerName $server -Credential $credential
    
    #Get CPU information
    Get-WmiObject -ComputerName $server -Class Win32_Processor | select SystemName,Name,NumberOfCores,NumberOfLogicalProcessors| Export-csv -Append -NoTypeInformation $exportDirectory\$exportFilePrefix`_CPUInformation.csv

    #Get memory information
    Get-WmiObject -ComputerName $server -Class Win32_PhysicalMemory | select PSComputerName,DeviceLocator,Manufacturer,PartNumber,SerialNumber,Speed,Capacity | Export-csv -Append -NoTypeInformation $exportDirectory\$exportFilePrefix`_MemoryInformation.csv

    #Get NICs information
    $nicInformationCommand= {Get-NetAdapter | where {$_.InterfaceDescription -notlike "*Microsoft*" -and $_.DriverDescription -notlike "*Hyper-V*"} | select Name,LinkSpeed,DriverProvider,DriverDescription,MacAddress,DriverVersion,DriverFileName,Status,ActiveMaximumTransmissionUnit,MtuSize,VLANID}
    Invoke-Command -Session $session -ScriptBlock $nicInformationCommand | Export-Csv -Append -Path  $exportDirectory\$exportFilePrefix`_NIC_Information.csv -NoTypeInformation

    #Get RDMA information
    $rdmaInformationCommand= {Get-NetAdapterRdma | select Name,InterfaceDescription,Enabled}
    Invoke-Command -Session $session -ScriptBlock $rdmaInformationCommand | Export-Csv -Append -Path  $exportDirectory\$exportFilePrefix`_NIC_RDMA_Information.csv -NoTypeInformation

    #Get DCB/QoS Information
    $netQoSInformationCommand= {Get-NetAdapterQos | select Name,ifDesc,Enabled,SystemName,OperationalFlowControl}
    Invoke-Command -Session $session -ScriptBlock $netQoSInformationCommand | Export-Csv -Append -Path  $exportDirectory\$exportFilePrefix`_NIC_QoS_Information.csv -NoTypeInformation

    #Get QoS Flow Control Information
    $netFlowControlInformationCommand= {Get-NetQosFlowControl}
    Invoke-Command -Session $session -ScriptBlock $netFlowControlInformationCommand | Export-Csv -Append -Path  $exportDirectory\$exportFilePrefix`_QoS_FlowControl_Information.csv -NoTypeInformation

    #Get Traffic Class Information
    $netQoSTrafficClassInformationCommand= {Get-NetQosTrafficClass}
    Invoke-Command -Session $session -ScriptBlock $netQoSTrafficClassInformationCommand | Export-Csv -Append -Path  $exportDirectory\$exportFilePrefix`_QoS_TrafficClass_Information.csv -NoTypeInformation

    #Get QoS Policy Information
    $netQoSPolicyInformationCommand= {Get-NetQosPolicy -PolicyStore ActiveStore}
    Invoke-Command -Session $session -ScriptBlock $netQoSPolicyInformationCommand | Export-Csv -Append -Path  $exportDirectory\$exportFilePrefix`_QoS_Policy_Information.csv -NoTypeInformation

    #Get SCSI controllers driver versions
    Get-WmiObject -ComputerName $server -Class Win32_PnpSignedDriver | where DeviceName -like "*LSI*" | select PSComputerName,Manufacturer,Location,DriverDate,DriverVersion,Description,InfName | Export-csv -Append -NoTypeInformation $exportDirectory\$exportFilePrefix`_SCSIControllerDriverInformation.csv
    
    #Get BIOS version information
    $biosInformation+=Get-WmiObject win32_bios -ComputerName $server | Export-Csv -Append -Path $exportDirectory\$exportFilePrefix`_BIOS_Information.csv -NoTypeInformation -Force
     
    #Get physical disk information
    Get-WmiObject -ComputerName $server -Namespace "root/Microsoft/Windows/Storage" -Class MSFT_PhysicalDisk |`
     select DeviceId,FriendlyName,Manufacturer,Model,SerialNumber,FirmwareVersion,EnclosureNumber,SlotNumber,Size |`
     Export-csv -Append -NoTypeInformation $exportDirectory\$exportFilePrefix`_PhysicalDiskInformation.csv
    
    #Get enclosure information
    Get-WmiObject -ComputerName $server -Namespace "root/Microsoft/Windows/Storage" -Class MSFT_StorageEnclosure | select FriendlyName,Manufacturer,Model,NumberOfSlots,SerialNumber,FirmwareVersion | Export-csv -Append -NoTypeInformation $exportDirectory\$exportFilePrefix`_EnclosureInformation.csv

    foreach($featureOrRoleToValidate in $featuresOrRolesToValidate)
    {
        $server | Validate-RecommendedUpdateStatus -featureOrRoleToValidate $featureOrRoleToValidate -recommendedUpdatesListPath ..\Windows\RecommendedUpdates.csv -strictFeatureValidation $true | Export-csv -Append -NoTypeInformation $exportDirectory\$exportFilePrefix`_Recommended_WindowsUpdates.csv
    }

    #Get core system file versions
    foreach($file in $files)
    {
        Get-ChildItem ("\\$server\" + $file) -ErrorAction SilentlyContinue| select @{Expression={$server};Label="ComputerName"},Name,@{Expression={$_.VersionInfo.FileMajorPart.ToString()+"."+$_.VersionInfo.FileMinorPart.ToString()+"."+$_.VersionInfo.FileBuildPart.ToString()+"."+$_.VersionInfo.FilePrivatePart.ToString()};Label="ProductVersion"},@{Expression={$_.VersionInfo.FileDescription};Label="FileDescription"} | Export-Csv -Path  $exportDirectory\$exportFilePrefix`_CoreFiles_Information.csv -NoTypeInformation -Append
    }

    Remove-PSSession -Session $session
}

#List all hotfixes installed
Get-HotFix -ComputerName $servers | select PSComputerName,@{Expression={$_.InstalledOn | Get-Date -Format "yyyy-MM-dd hh:mm:ss"};Label="InstalledOn"},Caption,Description,HotFixID,InstalledBy | Export-Csv -Path $exportDirectory\$exportFilePrefix`_Hotfix_Information.csv -NoTypeInformation -Force



